<template>
  <!-- 顶部导航栏 -->
  <el-header class="header">
    <div class="system-title">慢性病管理系统</div>
    <div class="header-right">
      <!-- 添加首页按钮 -->
      <el-button class="home-button" type="text" @click="$router.push('/home')">
        <el-icon><HomeFilled /></el-icon>
        首页
      </el-button>
      <!-- 这里可以放用户信息、通知等 -->
      <el-dropdown>
        <span class="el-dropdown-link">
          <el-avatar :size="30" :src="userAvatar" />
          <span style="margin-left: 10px">{{
            userStore.name || '管理员'
          }}</span>
        </span>
        <template #dropdown>
          <el-dropdown-menu>
            <el-dropdown-item>个人中心</el-dropdown-item>
            <el-dropdown-item @click="logout">退出登录</el-dropdown-item>
          </el-dropdown-menu>
        </template>
      </el-dropdown>
    </div>
  </el-header>

  <!-- 主体布局 -->
  <el-container class="main-container">
    <!-- 左侧菜单 -->
    <el-aside width="200px">
      <el-menu :default-active="$route.path" class="el-menu-vertical" router>
        <!-- 静态菜单项 - 根据权限显示 -->
        <el-sub-menu index="/statistics" v-permission="'statistics:data'">
          <template #title>
            <el-icon><DataLine /></el-icon>
            <span>统计报表</span>
          </template>
          <el-menu-item index="/home/statistics/data">数据统计</el-menu-item>
        </el-sub-menu>
        <!-- 用户管理下拉菜单 -->
        <el-sub-menu index="/user" v-permission="'user:list'">
          <template #title>
            <el-icon><User /></el-icon>
            <span>用户管理</span>
          </template>
          <el-menu-item index="/home/user/list" v-permission="'user:list'"
            >用户列表</el-menu-item
          >
          <el-menu-item index="/home/user/role" v-permission="'user:role'"
            >角色管理</el-menu-item
          >
          <el-menu-item
            index="/home/user/permission"
            v-permission="'user:permission'"
            >权限设置</el-menu-item
          >
        </el-sub-menu>

        <!-- 慢性病种管理下拉菜单 -->
        <el-sub-menu index="/disease" v-permission="'disease:list'">
          <template #title>
            <el-icon><FirstAidKit /></el-icon>
            <span>慢性病管理</span>
          </template>
          <el-menu-item index="/home/disease/list" v-permission="'disease:list'"
            >病种列表</el-menu-item
          >
          <el-menu-item
            index="/home/disease/category"
            v-permission="'disease:category'"
            >病种分类</el-menu-item
          >
        </el-sub-menu>

        <!-- 地区管理菜单项 -->
        <el-sub-menu index="/region" v-permission="'region:list'">
          <template #title>
            <el-icon><MapLocation /></el-icon>
            <span>地区管理</span>
          </template>
          <el-menu-item index="/home/region/list">行政区域管理</el-menu-item>
        </el-sub-menu>

        <!-- 医院菜单项 -->
        <el-sub-menu index="/hospital" v-permission="'hospital:list'">
          <template #title>
            <el-icon><OfficeBuilding /></el-icon>
            <span>医院管理</span>
          </template>
          <el-menu-item index="/home/hospital/list">医院管理</el-menu-item>
        </el-sub-menu>
        <!-- 病人管理菜单项 -->
        <el-sub-menu index="/patient" v-permission="'patient:list'">
          <template #title>
            <el-icon><UserFilled /></el-icon>
            <span>病人管理</span>
          </template>
          <el-menu-item index="/home/patient/list" v-permission="'patient:list'"
            >病人信息管理</el-menu-item
          >
          <el-menu-item
            index="/home/patient/register"
            v-permission="'patient:register'"
            >参合登记表</el-menu-item
          >
        </el-sub-menu>
        <!-- 报销管理菜单项 -->
        <el-sub-menu index="/reimbursement" v-permission="'reimbursement:list'">
          <template #title>
            <el-icon><Wallet /></el-icon>
            <span>报销管理</span>
          </template>
          <el-menu-item
            index="/home/reimbursement/list"
            v-permission="'reimbursement:list'"
            >报销记录表</el-menu-item
          >
          <el-menu-item
            index="/home/reimbursement/policy"
            v-permission="'reimbursement:policy'"
            >报销策略</el-menu-item
          >
        </el-sub-menu>

        <!-- 系统设置菜单项 -->
        <el-menu-item index="/setting" v-permission="'system:setting'">
          <el-icon><Setting /></el-icon>
          <span>系统设置</span>
        </el-menu-item>
      </el-menu>
    </el-aside>

    <!-- 右侧内容区域 -->
    <el-main class="main-content">
      <router-view />
    </el-main>
  </el-container>
</template>

<script lang="ts" setup>
import {
  User,
  Setting,
  FirstAidKit,
  MapLocation,
  Wallet,
  UserFilled,
  DataLine,
  OfficeBuilding,
  HomeFilled
} from '@element-plus/icons-vue'
import { useRoute, useRouter } from 'vue-router'
import { useUserStore } from '@/stores'

const $route = useRoute()
const $router = useRouter()
const userStore = useUserStore()
const userAvatar =
  'https://cube.elemecdn.com/3/7c/3ea6beec64369c2642b92c6726f1epng.png'
// 退出登录方法
const logout = () => {
  // 执行清除 token、用户信息等操作
  userStore.logout()
  $router.push('/login')
}
</script>

<style scoped>
/* 顶部导航栏样式 */
.header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  height: 60px;
  background: linear-gradient(
    90deg,
    var(--primary-dark) 0%,
    var(--primary-color) 100%
  );
  color: white;
  padding: 0 20px;
  box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1);
}

.system-title {
  font-size: 22px;
  font-weight: bold;
  letter-spacing: 1px;
  display: flex;
  align-items: center;

  &::before {
    content: '';
    display: inline-block;
    width: 24px;
    height: 24px;
    background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24' fill='white'%3E%3Cpath d='M19 3H5c-1.1 0-1.99.9-1.99 2L3 19c0 1.1.9 2 2 2h14c1.1 0 2-.9 2-2V5c0-1.1-.9-2-2-2zm-1 11h-4v4h-4v-4H6v-4h4V6h4v4h4v4z'/%3E%3C/svg%3E");
    margin-right: 10px;
  }
}

.header-right {
  display: flex;
  align-items: center;
}

.home-button {
  margin-right: 15px;
  color: white;
  font-size: 14px;

  :deep(.el-button) {
    color: white;

    &:hover {
      background-color: rgba(255, 255, 255, 0.1);
    }
  }

  .el-icon {
    margin-right: 5px;
  }
}

.el-dropdown-link {
  display: flex;
  align-items: center;
  color: white;
  cursor: pointer;
  transition: all 0.3s;

  &:hover {
    opacity: 0.9;
  }
}

/* 主体布局样式 */
.main-container {
  height: calc(100vh - 60px);
}

.el-aside {
  background-color: #fff;
  border-right: 1px solid #eaeaea;
  box-shadow: 2px 0 10px rgba(0, 0, 0, 0.05);
}

.el-menu-vertical {
  height: 100%;
  border-right: none;

  :deep(.el-sub-menu__title):hover {
    background-color: var(--primary-light);
  }

  :deep(.el-menu-item):hover {
    background-color: var(--primary-light);
  }

  :deep(.el-menu-item.is-active) {
    background-color: var(--primary-light);
    color: var(--primary-dark);
    font-weight: bold;

    &::before {
      content: '';
      position: absolute;
      left: 0;
      top: 0;
      width: 4px;
      height: 100%;
      background-color: var(--primary-color);
    }
  }

  :deep(.el-sub-menu.is-active .el-sub-menu__title) {
    color: var(--primary-dark);
  }

  :deep(.el-icon) {
    color: var(--primary-color);
  }
}

/* 右侧内容区域样式 */
.main-content {
  padding: 20px;
  background-color: var(--background-color);
  overflow-y: auto;
}
</style>
